//
//  MainTabBarController.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/18.
//

// MainTabBarController.swift
import UIKit

class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置代理
        self.delegate = self
        
        //配置标签栏外观
        configureTabbarAppearance()
        
        //设置视图控制器
        setupViewControllers()
    }
    
    private func configureTabbarAppearance() {
        //设置标签栏背景颜色
        tabBar.backgroundColor = AppTheme.Colors.tabbarBgColor
        
        //设置选中和未选中的图标及文字颜色
        let appearance = tabBar.standardAppearance
        appearance.configureWithOpaqueBackground()
        appearance.shadowColor = nil //去除分割线
        appearance.backgroundColor = AppTheme.Colors.tabbarBgColor
        
        //选中状态
        appearance.stackedLayoutAppearance.selected.iconColor = AppTheme.Colors.tabbarTextIcon_select_Color
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: AppTheme.Colors.tabbarTextIcon_select_Color]
        
        // 未选中状态
        appearance.stackedLayoutAppearance.normal.iconColor = AppTheme.Colors.tabbarTextIcon_default_Color
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: AppTheme.Colors.tabbarTextIcon_default_Color]
        
        tabBar.standardAppearance = appearance
        
        // 适配iOS 15+的scrollEdgeAppearance
        if #available(iOS 15.0, *) {
            tabBar.scrollEdgeAppearance = appearance
        }
    }
    
    private func setupViewControllers() {
        // 创建各个视图控制器
        let homeVC = HomeViewController()
        let mineVC = MineViewController()
        
        // 包装在导航控制器中
        let homeNav = MainNaviController(rootViewController: homeVC)
        let mineNav = MainNaviController(rootViewController: mineVC)
        
        // 设置导航控制器的标题和标签栏项
        homeNav.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named:"home_icon"), selectedImage: UIImage(named: "home_icon_select"))
        mineNav.tabBarItem = UITabBarItem(title: "搜索", image: UIImage(named: "mine_icon"), selectedImage: UIImage(named: "mine_icon_select"))
        
        // 设置视图控制器数组
        viewControllers = [homeNav, mineNav]
    }
}


// MARK: - UITabBarControllerDelegate
extension MainTabBarController {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        // 可添加切换标签的自定义逻辑
        return true
    }
}
